package lib;

import java.util.HashMap;
import java.util.List;
import java.util.Set;

import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.table.DefaultTableModel;

import model.Customer;
import model.Employee;
import model.Invoice;
import model.Mobile;
import model.Product;
import model.Supplier;
import model.Tablet;
import view.Home;
import view.customer.CustomerMainPanel;
import view.dialog.EmployeeChangePasswordDialog;
import view.employee.EmployeeMainPanel;
import view.product.ProductMainPanel;
import view.product.ProductSupplierPanel;
import view.statistic.StatisticPanelDay;
import dao.CustomerDAO;
import dao.EmployeeDAO;
import dao.LaptopDAO;
import dao.MobileDAO;
import dao.StatisticDAO;
import dao.SupplierDAO;
import dao.TabletDAO;

public class MyUtils {
	public static void switchView(Home home, final JPanel newPanel) {
		home.getPnMainDisplay().remove(home.getPnCurrentDisplay());
		home.setPn_current_display(newPanel);
		home.getPnMainDisplay().add(home.getPnCurrentDisplay());
		home.getPn_current_display().setBounds(0, 0, 911, 508);
		home.getPnMainDisplay().updateUI();
	}

	public static void doDispose(JDialog dia) {
		dia.dispose();
	}

	public static void LoadEmp(EmployeeChangePasswordDialog change) {
		DefaultTableModel dtm = new DefaultTableModel();
		dtm.addColumn("ID");
		dtm.addColumn("Name");
		dtm.addColumn("Birthday");
		dtm.addColumn("PersonId");
		dtm.addColumn("Phone");
		dtm.addColumn("Email");
		dtm.addColumn("Address");
		EmployeeDAO employeeDAO = new EmployeeDAO();
		for (Employee employee : employeeDAO.showAllEmployee()) {
			dtm.addRow(new Object[] { employee.getId(), employee.getName(),
					employee.getEmpsex(), employee.getEmpBirth(),
					employee.getEmpPerId(), employee.getPhone(),
					employee.getEmail(), employee.getAddress() });
		}

	}

	public static void doLoadEmployee(EmployeeMainPanel emp) {
		DefaultTableModel dtm = new DefaultTableModel();
		dtm.addColumn("ID");
		dtm.addColumn("Name");
		dtm.addColumn("Sex");
		dtm.addColumn("Birthday");
		dtm.addColumn("PersonId");
		dtm.addColumn("Phone");
		dtm.addColumn("Email");
		dtm.addColumn("Address");
		String empSex = "male";
		EmployeeDAO employeeDAO = new EmployeeDAO();
		for (Employee employee : employeeDAO.showAllEmployee()) {
			dtm.addRow(new Object[] { employee.getId(), employee.getName(),
					employee.getEmpsex(), employee.getEmpBirth(),
					employee.getEmpPerId(), employee.getPhone(),
					employee.getEmail(), employee.getAddress() });
			empSex = employee.getEmpsex();
		}

		emp.getCbbEmSex().setSelectedItem(empSex);
		emp.getTblEmListem().setModel(dtm);
	}

	public static void doLoadCustomer(CustomerMainPanel cus) {
		DefaultTableModel dtm = new DefaultTableModel();
		CustomerDAO cusDAO = new CustomerDAO();
		dtm.addColumn("ID");
		dtm.addColumn("Name");
		dtm.addColumn("Phone");
		dtm.addColumn("Email");
		dtm.addColumn("Address");
		for (Customer i : cusDAO.showAllCustomer()) {
			dtm.addRow(new Object[] { i.getId(), i.getName(), i.getPhone(),
					i.getEmail(), i.getAddress() });
		}
		cus.getTblCusListcustomer().setModel(dtm);
	}

	public static void doLoadMobile(ProductMainPanel pro) {
		DefaultTableModel dtm = new DefaultTableModel();
		MobileDAO mobileDAO = new MobileDAO();
		dtm.addColumn("Id");
		dtm.addColumn("Product name");
		dtm.addColumn("Supplier");
		dtm.addColumn("Purchase price");
		dtm.addColumn("Sale price");
		dtm.addColumn("Quantity");
		for (Mobile i : mobileDAO.showAllMobile()) {
			dtm.addRow(new Object[] { i.getId(), i.getName(),
					i.getSupplierName(), i.getPurPrice(), i.getSalPrice(),
					i.getQuantity() });
		}
		pro.getTblProdListproduct().setModel(dtm);
	}

	public static void doLoadTablet(ProductMainPanel pro) {
		DefaultTableModel dtm = new DefaultTableModel();
		TabletDAO tabletDAO = new TabletDAO();
		dtm.addColumn("Id");
		dtm.addColumn("Product name");
		dtm.addColumn("Supplier");
		dtm.addColumn("Purchase price");
		dtm.addColumn("Sale price");
		dtm.addColumn("Quantity");
		for (Tablet i : tabletDAO.showAllProduct()) {
			dtm.addRow(new Object[] { i.getId(), i.getName(),
					i.getSupplierName(), i.getPurPrice(), i.getSalPrice(),
					i.getQuantity() });
		}
		pro.getTblProdListproduct().setModel(dtm);
	}

	public static void doLoadLaptop(ProductMainPanel pro) {
		DefaultTableModel dtm = new DefaultTableModel();
		LaptopDAO laptopDAO = new LaptopDAO();
		dtm.addColumn("Id");
		dtm.addColumn("Product name");
		dtm.addColumn("Supplier");
		dtm.addColumn("Purchase price");
		dtm.addColumn("Sale price");
		dtm.addColumn("Quantity");
		for (Product i : laptopDAO.showAllProduct()) {
			dtm.addRow(new Object[] { i.getId(), i.getName(),
					i.getSupplierName(), i.getPurPrice(), i.getSalPrice(),
					i.getQuantity() });
		}
		pro.getTblProdListproduct().setModel(dtm);
	}

	public static void loadSupplier(ProductSupplierPanel sup) {
		SupplierDAO supDAO = new SupplierDAO();
		DefaultTableModel dtm = new DefaultTableModel();
		dtm.addColumn("ID");
		dtm.addColumn("Name");
		dtm.addColumn("Phone");
		dtm.addColumn("Address");
		for (Supplier i : supDAO.showAllSupplier()) {
			dtm.addRow(new Object[] { i.getId(), i.getName(), i.getPhone(),
					i.getAddress() });
			sup.getTblProdsupListsupplier().setModel(dtm);
		}
	}

	public static void doFillcbbProdSupplier(ProductMainPanel pro2) {
		SupplierDAO supDAO = new SupplierDAO();
		ProductMainPanel pro = new ProductMainPanel();
		List<Supplier> list = supDAO.showAllSupplier();
		for (Supplier i : list) {
			pro.getCbbProdSupplier().addItem(i.getName());
			;
		}
	}

	public static void loadStatistic(StatisticPanelDay sta) {
		DefaultTableModel dtm = new DefaultTableModel();
		dtm.addColumn("Date");
		dtm.addColumn("Total");
		StatisticDAO staDAO = new StatisticDAO();
		HashMap<String, Double> map = new HashMap<String, Double>();
		for (Invoice i : staDAO.showAllInvoice()) {
			if (map.containsKey(i.getDate()))
				map.put(i.getDate(), map.get(i.getDate()) + i.getTotal());
			else
				map.put(i.getDate(), i.getTotal());
		}

		Set<String> set = map.keySet();

		for (String i : set) {
			double total = map.get(i);
			dtm.addRow(new Object[] { i, total });
		}
		sta.getTbl_prodmain_listproduct1().setModel(dtm);
	}
}
